<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="授权码名称" prop="authorizationkeyName">
        <el-input v-model="queryParams.authorizationkeyName" placeholder="请输入授权码名称" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="授权码前缀" prop="authorizationkeyPrefix">
        <el-input v-model="queryParams.authorizationkeyPrefix" placeholder="请输入授权码前缀" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="授权码类型" prop="authorizationkeyType">
        <!-- <el-select v-model="queryParams.authorizationkeyType" placeholder="请选择授权码类型" clearable>
          <el-option v-for="dict in ${ dictType }" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select> -->
      </el-form-item>
      <el-form-item label="授权码长度" prop="authorizationkeyLength">
        <el-input v-model="queryParams.authorizationkeyLength" placeholder="请输入授权码长度" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="使用打印次数" prop="printNumber">
        <el-input v-model="queryParams.printNumber" placeholder="请输入使用打印次数" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="授权开始时间" prop="authorizationBegintime">
        <!-- <el-date-picker clearable v-model="queryParams.authorizationBegintime" type="date" value-format="YYYY-MM-DD"
          placeholder="选择授权开始时间">
        </el-date-picker> -->
      </el-form-item>
      <el-form-item label="授权结束时间" prop="authorizationEndtime">
        <!-- <el-date-picker clearable v-model="queryParams.authorizationEndtime" type="date" value-format="YYYY-MM-DD"
          placeholder="选择授权结束时间">
        </el-date-picker> -->
      </el-form-item>
      <el-form-item label="生成数量" prop="generateNumber">
        <el-input v-model="queryParams.generateNumber" placeholder="请输入生成数量" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="删除标志" prop="isDelete">
        <el-input v-model="queryParams.isDelete" placeholder="请输入删除标志" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入创建人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <!-- <el-date-picker clearable v-model="queryParams.createTime" type="date" value-format="YYYY-MM-DD"
          placeholder="选择创建时间">
        </el-date-picker> -->
      </el-form-item>
      <el-form-item label="更新人" prop="updatedBy">
        <el-input v-model="queryParams.updatedBy" placeholder="请输入更新人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedTime">
        <!-- <el-date-picker clearable v-model="queryParams.updatedTime" type="date" value-format="YYYY-MM-DD"
          placeholder="选择更新时间">
        </el-date-picker> -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['manage:code:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['manage:code:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['manage:code:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="Download" @click="handleExport"
          v-hasPermi="['manage:code:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="codeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="授权码名称" align="center" prop="authorizationkeyName">
        <!-- <template #default="scope">
          <dict-tag :options="${ column.dictType }" :value="scope.row.authorizationkeyName" />
        </template> -->
      </el-table-column>
      <el-table-column label="授权码前缀" align="center" prop="authorizationkeyPrefix">
        <!-- <template #default="scope">
          <dict-tag :options="${ column.dictType }" :value="scope.row.authorizationkeyPrefix" />
        </template> -->
      </el-table-column>
      <el-table-column label="授权码类型" align="center" prop="authorizationkeyType">
        <!-- <template #default="scope">
          <dict-tag :options="${ column.dictType }" :value="scope.row.authorizationkeyType" />
        </template> -->
      </el-table-column>
      <el-table-column label="授权码长度" align="center" prop="authorizationkeyLength">
        <!-- <template #default="scope">
          <dict-tag :options="${ column.dictType }" :value="scope.row.authorizationkeyLength" />
        </template> -->
      </el-table-column>
      <el-table-column label="使用打印次数" align="center" prop="printNumber">
        <!-- <template #default="scope">
          <dict-tag :options="${ column.dictType }" :value="scope.row.printNumber" />
        </template> -->
      </el-table-column>
      <el-table-column label="授权开始时间" align="center" prop="authorizationBegintime" width="180">
        <!-- <template #default="scope">
          <span>{{ parseTime(scope.row.authorizationBegintime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="授权结束时间" align="center" prop="authorizationEndtime" width="180">
        <!-- <template #default="scope">
          <span>{{ parseTime(scope.row.authorizationEndtime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="生成数量" align="center" prop="generateNumber">
        <!-- <template #default="scope">
          <dict-tag :options="${ column.dictType }" :value="scope.row.generateNumber" />
        </template> -->
      </el-table-column>
      <el-table-column label="删除标志" align="center" prop="isDelete">
        <!-- <template #default="scope">
          <dict-tag :options="${ column.dictType }" :value="scope.row.isDelete" />
        </template> -->
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy">
        <!-- <template #default="scope">
          <dict-tag :options="${ column.dictType }" :value="scope.row.createBy" />
        </template> -->
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <!-- <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updatedBy">
        <!-- <template #default="scope">
          <dict-tag :options="${ column.dictType }" :value="scope.row.updatedBy" />
        </template> -->
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedTime" width="180">
        <!-- <template #default="scope">
          <span>{{ parseTime(scope.row.updatedTime, '{y}-{m}-{d}') }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:code:edit']">修改</el-button>
          <el-button type="text" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:code:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改授权码对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="codeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="授权码名称" prop="authorizationkeyName">
          <el-input v-model="form.authorizationkeyName" placeholder="请输入授权码名称" />
        </el-form-item>
        <el-form-item label="授权码前缀" prop="authorizationkeyPrefix">
          <el-input v-model="form.authorizationkeyPrefix" placeholder="请输入授权码前缀" />
        </el-form-item>
        <el-form-item label="授权码类型" prop="authorizationkeyType">
          <!-- <el-select v-model="form.authorizationkeyType" placeholder="请选择授权码类型">
            <el-option v-for="dict in ${ dictType }" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="授权码长度" prop="authorizationkeyLength">
          <el-input v-model="form.authorizationkeyLength" placeholder="请输入授权码长度" />
        </el-form-item>
        <el-form-item label="使用打印次数" prop="printNumber">
          <el-input v-model="form.printNumber" placeholder="请输入使用打印次数" />
        </el-form-item>
        <el-form-item label="授权开始时间" prop="authorizationBegintime">
          <el-date-picker clearable v-model="form.authorizationBegintime" type="date" value-format="YYYY-MM-DD"
            placeholder="选择授权开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="授权结束时间" prop="authorizationEndtime">
          <el-date-picker clearable v-model="form.authorizationEndtime" type="date" value-format="YYYY-MM-DD"
            placeholder="选择授权结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生成数量" prop="generateNumber">
          <el-input v-model="form.generateNumber" placeholder="请输入生成数量" />
        </el-form-item>
        <el-form-item label="删除标志" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="创建人" prop="createBy">
          <el-input v-model="form.createBy" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker clearable v-model="form.createTime" type="date" value-format="YYYY-MM-DD" placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新人" prop="updatedBy">
          <el-input v-model="form.updatedBy" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedTime">
          <el-date-picker clearable v-model="form.updatedTime" type="date" value-format="YYYY-MM-DD" placeholder="选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Code" lang="ts">
import { listCode, getCode, delCode, addCode, updateCode } from '@/api/manage/code';
import { getCurrentInstance, reactive, ref, toRefs } from 'vue';

const { proxy } = getCurrentInstance() as any;

const codeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref('');


const form = ref<any>({});
const queryParams =ref({
	pageNum: 1,
	pageSize: 10,
	authorizationkeyName: null, authorizationkeyPrefix: null, authorizationkeyType: null, authorizationkeyLength: null, printNumber: null, authorizationBegintime: null, authorizationEndtime: null, generateNumber: null, isDelete: null, createBy: null, createTime: null, updatedBy: null, updatedTime: null
});
const rules = ref({});

/** 查询授权码列表 */
function getList() {
	loading.value = true;
	listCode(queryParams.value).then((response:any) => {
		codeList.value = response.data.list;
		total.value = response.total;
		loading.value = false;
	});
}

// 取消按钮
function cancel() {
	open.value = false;
	reset();
}

// 表单重置
function reset() {
	form.value = {
		id: null, authorizationkeyName: null, authorizationkeyPrefix: null, authorizationkeyType: null, authorizationkeyLength: null, printNumber: null, authorizationBegintime: null, authorizationEndtime: null, generateNumber: null, isDelete: null, createBy: null, createTime: null, updatedBy: null, updatedTime: null
	};
	proxy.resetForm('codeRef');
}

/** 搜索按钮操作 */
function handleQuery() {
	queryParams.value.pageNum = 1;
	getList();
}

/** 重置按钮操作 */
function resetQuery() {
	proxy.resetForm('queryRef');
	handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection:any) {
	ids.value = selection.map((item:any) => item.id);
	single.value = selection.length !== 1;
	multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
	reset();
	open.value = true;
	title.value = '添加授权码';
}

/** 修改按钮操作 */
function handleUpdate(row:any) {
	reset();
	const id = row.id || ids.value;
	getCode(id).then(response => {
		form.value = response.data;
		open.value = true;
		title.value = '修改授权码';
	});
}

/** 提交按钮 */
function submitForm() {
	proxy.$refs['codeRef'].validate((valid:boolean) => {
		if (valid) {
			if (form.value.id != null) {
				updateCode(form.value).then(response => {
					proxy.$modal.msgSuccess('修改成功');
					open.value = false;
					getList();
				});
			} else {
				addCode(form.value).then(response => {
					proxy.$modal.msgSuccess('新增成功');
					open.value = false;
					getList();
				});
			}
		}
	});
}

/** 删除按钮操作 */
function handleDelete(row:any) {
	const idList = row.id || ids.value;
	proxy.$modal.confirm('是否确认删除授权码编号为"' + idList + '"的数据项？').then(function () {
		return delCode(idList);
	}).then(() => {
		getList();
		proxy.$modal.msgSuccess('删除成功');
	}).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
	proxy.download('manage/code/exportByStream', {
		...queryParams.value
	}, `code_${new Date().getTime()}.xlsx`);
}

getList();
</script>
